package com.nt.train.controller;
import com.alibaba.fastjson.JSONObject;
import com.nt.train.service.BusinessService;
import com.nt.train.service.IPcsService;
import com.nt.train.service.add.AddBusinessServiceImpl;
import com.nt.train.util.expands.BusinessType;
import com.nt.train.util.expands.SpringContextUtil;
import com.nt.train.util.expands.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@RestController
public class HelloController {

    //@Autowired
    @Autowired(required = false)
    IPcsService pcsService ;


    @RequestMapping("/test2")
    public String sayHello(){
        return "hello spring boot!!" ;
    }

    @RequestMapping(value = "/second", method = RequestMethod.GET)
    public String second(){
        return "hello spring boot!!" ;
    }

    @RequestMapping(value = "/third", method = RequestMethod.POST)
    public String third(){
        return "hello spring boot!!" ;
    }

    @GetMapping("/hello2")
    public String sayHello2(){
        return "hello spring boot!!" ;
    }


    @RequestMapping("/hello")
    public String index(String name){
        return "Hello " +name+ ", I'm SpringBoot!";
    }

    /* 单元测试处理 */

    @GetMapping("//v1/users/1")
    public String testName(String name){
        System.out.println("name:"+name);
        return "{\"id\":1,\"name\":\"Oneby\"}" ;
    }


    @RequestMapping(value = "/v1/users/save",method = RequestMethod.POST)
    public String index(@RequestBody JSONObject json){
        //return JSONObject.toJSONString(json);
        return "{\"id\":1,\"name\":\"Oneby\"}";
    }


    /**
     * 模拟自定义注解处理demo
     * @return
     */
    @RequestMapping(value = "/testPrefix", method = RequestMethod.POST)
    public String testPrefix(){
        BusinessService businessService = null ;
        String businessType = null;
        businessService = (BusinessService) SpringUtils.getBusinessBean("business", AddBusinessServiceImpl.class.getName()
                , BusinessType.ADD);
        businessType = businessService.business();
        System.out.println("businessType:"+businessType);

        businessService =(BusinessService) SpringUtils.getBusinessBean("business","com.nt.train.service.update.UpdateBusinessServiceImpl",
                BusinessType.UPDATE);
        businessType = businessService.business();
        System.out.println("businessType1:"+businessType);

        String beanKey = String.join("-", "business", "com.nt.train.service.update.UpdateBusinessServiceImpl", BusinessType.UPDATE);
        businessService =(BusinessService)SpringContextUtil.getBean(beanKey);
        System.out.println("businessType2:"+ businessService.business());
        pcsService.testPcsInsert();

        return businessService.business() ;
    }

}
